home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Eagles Nest BBS 8
/
Eagles_Nest_Mac_Collection_Disc_8.TOAST
/
Developer Tools⁄Additions
/
IntermediatC
/
Ships 2 #8
/
Teacher's ship 2.c
< prev
Wrap
C/C++ Source or Header
|
1990-11-02
|
4KB
|
175 lines
/*
GIVEN:
#include <math.h>
#define RADIANS_PER_DEGREE 0.017453292519943
typedef struct ship_type
{
int hull_number;
position_type posit;
double course;
double speed;
} ship_type;
CREATE THIS PROGRAM:
Create NO_OF_SHIPS (define anywhere from 10 to 20) at random positions -100 to 100
(longitude and latitude will be of type double) with randomly assigned courses from
0 up to and including 359 degrees and speed from 10 to 30 knots. North is 0 degrees,
and degrees increase clockwise (a diagram is provided below). Assign the hull number
any way you desire, but all ships must have a different hull number. Display all
ship information. This includes the hull number, position (longitude and latitude),
course, and speed.
Now, let the ships sail (they move the assigned speed in the assigned direction).
Ignore units (thus, a ship at 0 longitude and 0 latitude with a course of 180 and
speed of 15 would move 15 south to 0 longitude and -15 latitude). The ships are to
each move 5 times. After each set of moves, eliminate any ship that is no longer in
the region of -100 to 100 longitude and -100 to 100 latitude. Thus NO_OF_SHIPS are
to move, the program will search to see if any ships are no longer in the boundaries,
eliminate tracking those ships in the future, and proceed to the next set of movements.
As said earlier, the ships each move 5 times (unless no longer being tracked).
Those ships still in the boundaries after 5 moves are to be listed as above in the same
file.
DIAGRAM OF DIRECTIONS
North
0 degrees
|
|
|
West 270 degrees --------- 90 degrees East
|
|
|
180 degrees
South
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NO_OF_SHIPS 10
#define NO_OF_TRIALS 5
#define RADS_PER_DEG 0.017453292519943
#define random(a,b) ((rand() % ((b) - (a))) + (a))
typedef struct position_type
{
double latitude;
double longitude;
} position_type;
typedef struct ship_type
{
int hull_number;
position_type posit;
double course;
double speed;
} ship_type;
typedef struct fleet_type
{
int ship_count;
ship_type ship[NO_OF_SHIPS];
} fleet_type;
static fleet_type create_fleet (fleet_type fleet);
static fleet_type move_ships (fleet_type fleet);
static fleet_type remove_ship (fleet_type fleet, int ship_index);
static void display_ships (fleet_type fleet);
main()
{
fleet_type fleet;
int i;
fleet = create_fleet(fleet);
display_ships(fleet);
for (i = 0; i < NO_OF_TRIALS; i++)
{
fleet = move_ships(fleet);
display_ships(fleet);
}
}
static fleet_type create_fleet (fleet_type fleet)
{
int i;
fleet.ship_count = NO_OF_SHIPS;
for (i = 0; i < NO_OF_SHIPS; i++)
{
fleet.ship[i].hull_number = i;
fleet.ship[i].posit.latitude = random(-100, 100);
fleet.ship[i].posit.longitude = random(-100, 100);
fleet.ship[i].course = random(0, 360);
fleet.ship[i].speed = random(10, 30);
}
return fleet;
}
static fleet_type move_ships (fleet_type fleet)
{
int i;
for (i = 0; i < fleet.ship_count; i++)
{
fleet.ship[i].posit.latitude += fleet.ship[i].speed *
cos(fleet.ship[i].course * RADS_PER_DEG);
fleet.ship[i].posit.longitude += fleet.ship[i].speed *
sin(fleet.ship[i].course * RADS_PER_DEG);
if (!((-100. <= fleet.ship[i].posit.latitude) &&
(fleet.ship[i].posit.latitude < 100.0)) ||
!((-100. <= fleet.ship[i].posit.longitude) &&
(fleet.ship[i].posit.longitude < 100.0)))
fleet = remove_ship(fleet, i);
}
return fleet;
}
static fleet_type remove_ship (fleet_type fleet, int ship_index)
{
int i;
for (i = ship_index; i < fleet.ship_count - 1; i++)
fleet.ship[i] = fleet.ship[i + 1];
fleet.ship_count--;
return fleet;
}
static void display_ships (fleet_type fleet)
{
int i;
for (i = 0; i < fleet.ship_count; i++)
{
printf("%d %8.3lf %8.3lf %8.3lf %8.3lf\n",
fleet.ship[i].hull_number,
fleet.ship[i].posit.latitude,
fleet.ship[i].posit.longitude,
fleet.ship[i].course,
fleet.ship[i].speed);
}
printf("\n");
}